package com.knight.hadoop.day08.flow;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class FlowDemo {

	static class FlowMapper extends Mapper<LongWritable, Text, Text, FlowModel> {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

			// key是读取偏移量
			String line = value.toString();
			String[] values = line.split("\t");

			String phoneNum = values[1];// 手机号
			Long upFlow = Long.parseLong(values[values.length - 3]);
			Long dwFlow = Long.parseLong(values[values.length - 2]);

			context.write(new Text(phoneNum), new FlowModel(upFlow, dwFlow));
		}
	}

	static class FlowReducer extends Reducer<Text, FlowModel, Text, FlowModel> {
		@Override
		protected void reduce(Text key, Iterable<FlowModel> values, Context context)
				throws IOException, InterruptedException {

			long upFlow = 0;
			long dwFlow = 0;
			for (FlowModel flowModel : values) {
				upFlow += flowModel.getUpFlow();
				dwFlow += flowModel.getDwFlow();
			}

			context.write(key, new FlowModel(upFlow, dwFlow));
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration configuration = new Configuration();

		Job job = Job.getInstance(configuration);

		// 指定本程序的jar包所在的本地路径
		job.setJarByClass(FlowDemo.class);

		// 设置mapper任务执行类
		job.setMapperClass(FlowMapper.class);
		// 设置reducer任务执行类
		job.setReducerClass(FlowReducer.class);
		
		//设置自定义的数据分发组件
		//job.setPartitionerClass(ProvincePartitioner.class);
		//设置相应分区数量的reduceTask
		//job.setNumReduceTasks(5);
		
		
		// 指定mapper输出数据的kv类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(FlowModel.class);

		// 指定最终输出数据的kv类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(FlowModel.class);

		// 指定输入参数的目录
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		// 指定输出参数的目录
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		// 将job配置的参数以及job所用的java类所在的jar包，提交到yarn去运行
		/* job.submit(); */
		// 但是我们一般用这个，因为可以等待运行结果返回，查看运行流程
		boolean res = job.waitForCompletion(true);
		System.exit(res ? 0 : 1);
	}

}
